﻿using Microsoft.VisualStudio.Modeling;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Project.Co_DataTransformationPrototype
{
    public static partial class Operators
    {
        public partial class Operator
        {
            private bool checkMoveOperator(IList<EntityAttribute> attributesToMove, Entity targetEntity)
            {
                // TODO: Implement pre-condition check
                return (attributesToMove != null && targetEntity != null);
            }

            private Entity getTargetEntity(EntityAttribute[] selectedAttributes, out System.Windows.Forms.DialogResult dlgResult)
            {
                Entity target = null;

                UI.MoveDialogBox dlgBox = new UI.MoveDialogBox(opStore.ElementDirectory.FindElements<EntityShape>().ToArray(), selectedAttributes);
                dlgResult = dlgBox.ShowDialog();
                
                if (dlgResult == System.Windows.Forms.DialogResult.OK)
                    target = dlgBox.targetEntity;

                return target;
            }

            public Operator MoveOperator(IList<EntityAttribute> attributesToMove, System.Guid targetEntityID)
            {
                return MoveOperator(attributesToMove, getEntity(targetEntityID));
            }

            public Operator MoveOperator(IList<EntityAttribute> attributesToMove, Entity targetEntity)
            {
                System.Windows.Forms.DialogResult dlgResult = new System.Windows.Forms.DialogResult();

                // If the target entity is not provided, ask the user for it
                if (targetEntity == null)
                    targetEntity = getTargetEntity(attributesToMove.ToArray(), out dlgResult);

                // Skip operation if user cancels
                if (dlgResult == System.Windows.Forms.DialogResult.Cancel)
                    return this;

                // Skip operation if needed parameters are invalid
                if (!checkMoveOperator(attributesToMove, targetEntity))
                {
                    System.Windows.Forms.MessageBox.Show("Provided parameters failed the validation process, therefore this operator will be skipped.", "Move Operator Validation", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    return this;
                }

                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Moving attributes"))
                {
                    Entity source = null;
                    foreach (EntityAttribute attribute in attributesToMove)
                    {
                        source = attribute.Entity as Entity;
                        targetEntity.EntityAttributes.Add(attribute.Copy() as EntityAttribute);
                        source.EntityAttributes.Remove(attribute);
                    }
                    t.Context.Add(GetMoveOpTransactionKey, source);
                    t.Commit();
                }
                return this;
            }
        }
    }
}
